package com.appsindy.common.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;

public class StringUtils {
	
    private StringUtils(){ // Proposed of this class for be utility that no need to new instance - Chai
    }
    
    public static boolean isEmpty(String s) {
        return s == null || s.trim().equals("");
    }

    public static boolean isNumberEmpty(String s) {
        return s == null || s.trim().equals("") || s.trim().equals("0");
    }

    public static String toString(String str) {
        return (str == null ? "" : str.trim());
    }

    public static String toString(String str, String def) {
        return (str == null ? toString(def) : str.trim());
    }

    public static String toSqlString(String str) {
        return toString(str).replaceAll("'", "''");
    }

    public static int toInt(String str) {
        int result = 0;
        try {
            result = Integer.parseInt(toString(str).replaceAll(",", ""));
        } catch (Throwable t) {
          
        }
        return result;
    }

    public static int toInt(String str, int def) {
        int result = def;
        try {
            result = Integer.parseInt(toString(str).replaceAll(",", ""));
        } catch (Throwable t) {
            
        }
        return result;
    }

    public static int toInt(Integer i) {
        int result = 0;
        try {
            result = i;
        } catch (Throwable t) {
          
        }
        return result;
    }

    public static long toLong(String str) {
        long result = 0;
        try {
            result = Long.parseLong(toString(str).replaceAll(",", ""));
        } catch (Throwable t) {
           
        }
        return result;
    }

    public static long toLong(String str, long def) {
        long result = def;
        try {
            result = Long.parseLong(toString(str).replaceAll(",", ""));
        } catch (Throwable t) {
           
        }
        return result;
    }

    public static long toLong(Long l) {
        long result = 0;
        try {
            result = l;
        } catch (Throwable t) {
           
        }
        return result;
    }

    public static double toDouble(String str) {
        double result = 0;
        try {
            result = Double.parseDouble(toString(str).replaceAll(",", ""));
        }  catch (Throwable t) {
          
        }
        return result;
    }

    public static double toDouble(String str, double def) {
        double result;
        try {
            result = Double.parseDouble(toString(str).replaceAll(",", ""));
        } catch (Throwable t) {
            result = def;
        }
        return result;
    }

    public static double toDouble(Integer d) {
        double result;
        try {
            result = d;
        } catch (Throwable t) {
            result = 0;
        }
        return result;
    }

    public static double toDouble(Double d) {
        double result;
        try {
            result = d;
        } catch (Throwable t) {
            result = 0d;
        }
        return result;
    }

    public static boolean toBoolean(Boolean b) {
        boolean result = false;
        try {
            result = b;
        } catch (Throwable t) {
            
        }
        return result;
    }

    public static Byte toByte(String str) {
        Byte result = 0;
        try {
            result = Byte.parseByte(toString(str).replaceAll(",", ""));
        } catch (Throwable t) {
           
        }
        return result;
    }
    
    public static boolean toBoolean (String str) {
        boolean result = false;
        try {
            result = Boolean.parseBoolean(toString(str).replaceAll(",", ""));
        } catch (Throwable t) {
          
        }
        return result;
    }
    
    public static String parseString(Object[] objs, int index) {
        if (objs == null || objs.length <= index) {
            return null;
        }
        return parseString(objs[index]);
    }

    public static String parseString(Object obj) {
        if (obj == null || obj instanceof String) {
            return (String) obj;
        }
        return obj.toString();
    }

    public static Integer parseInteger(Object[] objs, int index) {
        if (objs == null || objs.length <= index) {
            return null;
        }
        return parseInteger(objs[index]);
    }

    public static Integer parseInteger(Object obj) {
        if (obj == null || obj instanceof Integer) {
            return (Integer) obj;
        }
        return toInt(obj.toString());
    }

    public static Long parseLong(Object[] objs, int index) {
        if (objs == null || objs.length <= index) {
            return null;
        }
        return parseLong(objs[index]);
    }

    public static Long parseLong(Object obj) {
        if (obj == null || obj instanceof Long) {
            return (Long) obj;
        }
        return toLong(obj.toString());
    }

    public static Double parseDouble(Object[] objs, int index) {
        if (objs == null || objs.length <= index) {
            return null;
        }
        return parseDouble(objs[index]);
    }

    public static Double parseDouble(Object obj) {
        if (obj == null || obj instanceof Double) {
            return (Double) obj;
        }
        return toDouble(obj.toString());
    }

    public static Date parseDate(Object[] objs, int index) {
        if (objs == null || objs.length <= index) {
            return null;
        }
        return parseDate(objs[index]);
    }

    public static Date parseDate(Object obj) {
        if (obj == null || obj instanceof Date) {
            return (Date) obj;
        }
        return DateUtils.createDate(obj.toString(), false);
    }

    public static Byte parseByte(Object obj) {
        if (obj == null || obj instanceof Byte) {
            return (Byte) obj;
        }
        return toByte(obj.toString());
    }
    
    public static boolean parseBoolean(Object obj) {
        if (obj == null || obj instanceof Boolean) {
            return (Boolean) obj;
        }
        return toBoolean(obj.toString());
    }
    
    public static String convertLang(String lang) {
        if (lang != null && lang.equals("th_TH")) {
            return "THA";
        } else {
            return "ENG";
        }
    }

    public static boolean isTH(String lang) {
        return lang != null && lang.equals("th_TH");
    }

    public static String[] toArrayString(String str, String concat) {
        StringTokenizer token = new StringTokenizer(str, concat);
        String[] result = new String[token.countTokens()];
        for (int i = 0; i < result.length; i++) {
            result[i] = token.nextToken();
        }
        return result;
    }

    public static Hashtable arrayToHashtable(String[] array) {
        Hashtable h = new Hashtable();
        for (int i = 0; array != null && i < array.length; i++) {
            if (!isEmpty(array[i])) {
                h.put(toString(array[i]), "");
            }
        }
        return h;
    }

    public static String arrayToString(String[] array) {
        Hashtable h = arrayToHashtable(array);
        if (h.isEmpty()) {
            return null;
        } else {
            String result = " (";
            int index = 0;
            Enumeration enu = h.keys();
            while (enu.hasMoreElements()) {
                String key = (String) enu.nextElement();
                if (index == h.size() - 1) {
                    result += "'" + key + "'";
                } else {
                    result += "'" + key + "', ";
                }
                index++;
            }
            result += ") ";
            return result;
        }
    }

    public static String[] intersectArray(String[] str1, String[] str2) {
        Hashtable h1 = arrayToHashtable(str1);
        Hashtable h2 = arrayToHashtable(str2);

        List list = new ArrayList();
        Enumeration enu = h1.keys();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            if (h2.containsKey(key)) {
                list.add(key);
            }
        }

        String[] result = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = (String) list.get(i);
        }
        return result;
    }

    public static String[] unionArray(String[] str1, String[] str2) {
        Hashtable h1 = arrayToHashtable(str1);
        Hashtable h2 = arrayToHashtable(str2);

        Enumeration enu = h1.keys();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            h2.put(key, "");
        }

        List list = new ArrayList();
        enu = h2.keys();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            list.add(key);
        }

        String[] result = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            result[i] = (String) list.get(i);
        }
        return result;
    }

    public static boolean isInArray(String[] str1, String[] str2) {
        Hashtable h1 = arrayToHashtable(str1);
        Hashtable h2 = arrayToHashtable(str2);

        Enumeration enu = h1.keys();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            if (h2.containsKey(key) == false) {
                return false;
            }
        }
        return true;
    }

    public static boolean isInArray(String str1, String[] str2) {
        return isInArray(new String[]{str1}, str2);
    }

    // Convert such example HELLO_WORLD -> Hello World
    public static String charCap(String s) {
        StringBuffer bf = new StringBuffer();
        String str[] = s.split("_");
        for (int i = 0; i < str.length; i++) {
            bf.append(upperFirstChar(str[i].toLowerCase())).append(" ");
        }

        return bf.toString();
    }

    public static String lowerFirstChar(String s) {
        return s.substring(0, 1).toLowerCase() + s.substring(1, s.length());
    }

    public static String upperFirstChar(String s) {
        return s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
    }
    
    public static String fixCharacterLength(String srcString, char charForFix, int fixLen, boolean isFixAtFront){
        int len = srcString!=null?srcString.length():0;
        
        StringBuffer fixChars = new StringBuffer("");
        for(int i=len; i<fixLen; i++){
            fixChars.append(charForFix);
        }
        
        if(isFixAtFront){
            return fixChars.append((srcString!=null?srcString:"")).toString();
        }
        return (srcString!=null?srcString:"") + fixChars.toString();
    }
    
}
